<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/2
 * Time: 10:45
 */

namespace app\wap\controller;

use think\Db;
use think\Exception;
use think\log;

class Coupon extends Basewap
{

    public function receive(){
        $shu=[];
        $member_id=input('member_id');
        //优惠券ID
        $coupon_id=input('coupon_id');
        $gen1= db('coupon')->where('coupon_id',$coupon_id)->find();
        //追加无限量的优惠券判断
        if(empty($gen1['num_types'])){
            $gen1['num_types']=2;
        }
        if (($gen1['coupon_quantity']-$gen1['coupon_alreadyreceived'])<=0 && $gen1['num_types']==2){
            $g= db('coupon')->where('coupon_id',$coupon_id)->update(['coupon_status'=>3]);
            $shu=[
                'code'=>0,
                'msg'=>'领取失败,旅行红包抢光了下次再来吧！',
            ];
            return json_encode($shu);
        }
        Db::startTrans();
        try{
            //对领取记录入库
            $da=db('coupondetails')->insert([
                'member_id'=>$member_id,
                'coupon_id'=>$coupon_id,
                'starttime'=>time()
            ]);
            $shu=db('coupondetails')->where('coupon_id',$coupon_id)->count();
            $gen= db('coupon')->where('coupon_id',$coupon_id)->update(['coupon_alreadyreceived'=>$shu]);
            // 提交事务
            Db::commit();

            $shu=[
                'code'=>0,
                'msg'=>'领取成功',
            ];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $shu=[
                'code'=>0,
                'msg'=>'领取失败',
            ];
        }


        return json_encode($shu);


    }



    public function following(){//查询当前商品下可用的优惠券
        $d=[];
        $goods_id= input('goods_id');
        $member_id=input('member_id');

        $classify_id=Db::name('goods')->where(['goods_id'=>$goods_id])->find()['classify_id'];
        $c= db('coupondetails')->where('member_id',$member_id)->where('usestatus',0)->select();
        if(!empty($c)){
            foreach ($c as $s){
                $t=  db('coupon')->where('coupon_id',$s['coupon_id'])->find();
                if(empty($t)){
                    continue;
                }

                $classify_idArr = [];
                if(!empty($t['classify_id'])){
                    $classify_idArr = explode(",",$t['classify_id']);
                }

                if ($t['coupon_userange']==$goods_id || $t['coupon_userange']=='999999' || in_array('999999',$classify_idArr) || in_array($classify_id,$classify_idArr)){
                    if ($t['end_usetime']>time() || $t['time_use_type'] == 1) {

                        if ($t['time_use_type'] == 3){
                            $t['etart_usetime']     =   $s['starttime'];
                            $t['end_usetime']       =   $s['starttime']+(86400*$t['end_useday']);
                        }

                        $d[] = [
                            'id' => $s['id'],
                            'coupon_discount'       => $t['coupon_discount'],
                            'coupon_name'           => $t['coupon_name'],
                            'coupon_types'          => $t['coupon_types'],
                            'end_usetime'           => $t['end_usetime'],
                            'etart_usetime'         => $t['etart_usetime'],
                            'coupon_usecondition'   => $t['coupon_usecondition'],
                            'time_use_type'         => $t['time_use_type'],
                        ];
                    }else{
                        db('coupondetails')->where('id',$s['id'])->update([
                            'usestatus'=>2
                        ]);
                    }
                }
            }
        }
        $data=[];
        $shu=[];

        $Kbxcoupon =new Kbxcoupon();
        $da=$Kbxcoupon->usercoupon($member_id);

        $log1[]=[
            '查询优惠券返回'=>$da,
        ];
        Log::record($log1);

        if (!empty($da)){
            $da=json_decode($da,true);
            foreach ($da as $i){
                if ($i['locked']==false && $i['amount']!=0){
                    $data[] = [
                        "coupon_id" => $i['_id'],
                        "coupon_name" => '',
                        "discount" => '',//折扣
                        "full_reduction" => $i['amount'],//满减
                        "validityperiod" => '无限期',//有效期
                        "coupon_usecondition" => !empty($i['restrictions']['minPrice'])?$i['restrictions']['minPrice']:0,//使用条件
                        "type" =>'KBX',
                    ];
                }
            }
        }


        if (!empty($d)){
            foreach ($d as $v) {
                if($v['etart_usetime'] > time()){
                    continue;
                }
                if ($v['end_usetime'] > time() || $v['time_use_type'] == 1) {
                    if ($v['coupon_types'] == 1) {
                        $v['full_reduction'] = '';
                    } else if ($v['coupon_types'] == 2) {
                        $v['full_reduction'] = $v['coupon_discount'];
                        $v['coupon_discount'] = '';
                    }
                    if($v['time_use_type'] == 1){
                        $validityperiod='无期限';
                    }else{
                        $validityperiod=date('Y.m.d', $v['etart_usetime']) . ' - ' . date('Y.m.d', $v['end_usetime']);
                    }
                    $data[] = [
                        "coupon_id" => $v['id'],
                        "coupon_name" => $v['coupon_name'],
                        "discount" => $v['coupon_discount'],//折扣
                        "full_reduction" =>(float)$v['full_reduction'],//满减
                        "validityperiod" => $validityperiod,//使用期限
                        "coupon_usecondition" => $v['coupon_usecondition'],//使用条件
                        "type" =>'OWN',
                    ];
                }
            }
        }

        $shu=[
            'code'=>0,
            'msg'=>'查询成功',
            'data'=>$data
        ];

        $log[]=[
            '查询优惠券'=>$shu,
        ];
        Log::record($log);

        return json_encode($shu);
    }


    /*
     * 营销优惠券领取整理
    * @access public
    * @param mixed $menber  会员id
    * @param mixed $mk_id 营销id
    * @return array
     */
    public function couponMarket($member,$mk_id,$order_id='',$goods_id=''){

        $arr=['code'=>0,'msg'=>'获取旅行红包成功','data'=>''];
        $count_list_market=[];//可领旅行红包

        $data=Db::name('coupon_marketing')->where(['mk_id'=>$mk_id])->find();
        //先判断活动状态
        if($data['status'] == 2 ){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }
        if($data['time_chose'] == 2 && $data['endtime'] < time()){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }
        if($data['time_chose'] == 2 && $data['starttime'] > time()){
            $arr=['code'=>1,'msg'=>'活动尚未开始','data'=>''];
            return $arr;
        }
        //获取优惠券
        if(empty($data['coupon_ids'])){
            $arr=['code'=>1,'msg'=>'旅行红包没有啦','data'=>''];
            return $arr;
        }else{
            $count_list_market=explode(',',$data['coupon_ids']);
        }

        //如果send_type为1 即领取,返回已领券的内容，否则返回需要凑单的人数或者人数判断
        if($data['send_type'] == 2 && $mk_id == 2){
            $temp_share=Db::name('share_register')->where(['member_id'=>$member,'order_id'=>$order_id ])->select();
            if(empty($temp_share)){
                //添加
                if(count($count_list_market) > 1){
                    $list_temp_all=[];
                    foreach ($count_list_market as $k){
                        $list_temp=[
                            'member_id'=>$member,
                            'createtime'=>time(),
                            'order_id'=>$order_id,
                            'status'=>1,
                            'sharenum'=>$data['share'],
                            'coupon_id'=>$k, // 追加优惠券id
                            'goods_id'=>$goods_id,
                            'type'=>1
                        ];
                        array_push($list_temp_all,$list_temp);
                    }
                    Db::name('share_register')->insertAll($list_temp_all);
                }else{
                    $list_temp=[
                        'member_id'=>$member,
                        'createtime'=>time(),
                        'order_id'=>$order_id,
                        'status'=>1,
                        'sharenum'=>$data['share'],
                        'coupon_id'=>$count_list_market[0],// 追加优惠券id
                        'goods_id'=>$goods_id,
                        'type'=>1
                    ];
                    Db::name('share_register')->insert($list_temp);
                }
                $arr=['code'=>1,'msg'=>'还需邀请'.$data['share'].'人参与注册即可领券哦','data'=>''];
                return $arr;

            }else{
                //领取取出来，统一在优惠券领取页进行领取
                $arr=['code'=>0,'msg'=>'分享成功，快去"旅行红包领取"里面查看奖励吧','data'=>''];
                return $arr;
            }
        }

        //评论直接写入券记录
        if($mk_id == 3){
            $temp_share=Db::name('share_register')->where(['member_id'=>$member,'order_id'=>$order_id ])->select();
            if(empty($temp_share)){
                //添加
                if(count($count_list_market) > 1){
                    $list_temp_all=[];
                    foreach ($count_list_market as $k){
                        $list_temp=[
                            'member_id'=>$member,
                            'createtime'=>time(),
                            'order_id'=>$order_id,
                            'status'=>2,
                            'sharenum'=>1,
                            'coupon_id'=>$k, // 追加优惠券id
                            'goods_id'=>$goods_id,
                            'type'=>2
                        ];
                        array_push($list_temp_all,$list_temp);
                    }
                    Db::name('share_register')->insertAll($list_temp_all);
                }else{
                    $list_temp=[
                        'member_id'=>$member,
                        'createtime'=>time(),
                        'order_id'=>$order_id,
                        'status'=>2,
                        'sharenum'=>1,
                        'coupon_id'=>$count_list_market[0],// 追加优惠券id
                        'goods_id'=>$goods_id,
                        'type'=>2
                    ];
                    Db::name('share_register')->insert($list_temp);
                }
                $arr=['code'=>0,'msg'=>'分享成功，快去‘旅行红包领取’里面查看奖励吧','data'=>''];
                return $arr;

            }else{
                //领取取出来，统一在优惠券领取页进行领取
                $arr=['code'=>0,'msg'=>'分享成功，快去‘旅行红包领取’里面查看奖励吧','data'=>''];
                return $arr;
            }
        }

        $time=time();
        $w['coupon_status']=2;
        $w['etart_receivetime']=['<',$time];
        $w['end_receivetime']=['>',$time];
        $coupon_list=Db::name('coupon')->where('coupon_id','IN',$data['coupon_ids'])->where($w)->select();

        $re_data=[];//返回优惠券护具
        $set_data=[];// 进行领取数据写入
        //对每个优惠券的数量进行判断
        foreach ($coupon_list as $k){
            $temp=[
                'member_id'=>$member,
                'coupon_id'=>$k['coupon_id'],
                'starttime'=>time(),
                'usestatus'=>0
            ];

            $re_temp=[
                'coupon_id'=>$k['coupon_id'],
                'coupon_name'=>$k['coupon_name'],
                'coupon_num'=>1, //领取数量 暂定为1
                'coupon_discount'=>$k['coupon_discount'], //折扣或满减金额
                'coupon_type'=>$k['coupon_types']  //1=折扣,2=满减
            ];

            $k['num_types']=empty($k['num_types'])?2:$k['num_types'];
            //无限量 不进行验证
            if($k['num_types'] == 1){
                array_push($re_data,$re_temp);
                array_push($set_data,$temp);
            }else{
                if($k['coupon_alreadyreceived'] < $k['coupon_quantity']){
                    array_push($re_data,$re_temp);
                    array_push($set_data,$temp);

                }
            }
        }
        if(empty($re_data)) {
            $arr = ['code' => 1, 'msg' => '旅行红包已经被抢光了', 'data' => ''];
            return $arr;
        }else{
            //领取
            Db::startTrans();
            try{
                //对领取记录入库
                $da=db('coupondetails')->insertAll($set_data);
                //领取数量统计
                foreach ($set_data as $v){
                    $shu=db('coupondetails')->where('coupon_id',$v['coupon_id'])->count();
                    $gen= db('coupon')->where('coupon_id',$v['coupon_id'])->update(['coupon_alreadyreceived'=>$shu]);
                }

                if($data['send_type'] == 2 ){  //修改后， == 2的领取不走此方法
                    Db::name('share_register')->where(['member_id'=>$member,'order_id'=>$order_id ])->update(['status'=>3]);
                }

                // 提交事务
                Db::commit();
                $arr = ['code' => 0, 'msg' => '领取成功', 'data' =>$re_data];
                return $arr;

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $arr = ['code' => 1, 'msg' => '领取失败', 'data' =>''];
                return $arr;
            }
        }

    }


    //获取下单反优惠券
    public function getCoupon(){
        $member = input('member_id');
        $order_id = input('order_id');
        $goods_id = input('goods_id');
        if(empty($member) || empty($order_id)){
            $arr=['code'=>1,'msg'=>'缺少参数member_id或order_id','data'=>''];
            return json_encode($arr);
        }
        $res=$this->couponMarket($member,2,$order_id,$goods_id);
        return json_encode($res);
    }

    //通过个人中心的优惠券领取进行领取
    public function getCouponUser(){
        $coupon_id = input('coupon_id');
        $member = input('member_id');
        $order_id = input('order_id');
        $sharenum = input('sharenum');

        if(empty($coupon_id) || empty($member)){
            $arr=['code'=>1,'msg'=>'coupon_id或member_id字段错误','data'=>''];
            return json_encode($arr);
        }
        $status=Db::name('share_register')->where(['id'=>$coupon_id,'status'=>2])->find();
        if(empty($status)){
            $arr=['code'=>1,'msg'=>'该旅行红包已领取或不存在','data'=>''];
            return json_encode($arr);
        }

        $mk_id = 2;
        if($status['type']==1){//下单分享
            $mk_id = 2;
        }elseif($status['type']==2){//评论分享
            $mk_id = 3;
        }elseif($status['type']==3){//新年活动
            $mk_id = 4;
        }elseif($status['type']==4){//常驻邀新
            $mk_id = 5;
        }
        $data=Db::name('coupon_marketing')->where(['mk_id'=>$mk_id])->find();
        //先判断活动状态
        if($data['status'] == 2 ){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return json_encode($arr);
        }
        if($data['time_chose'] == 2 && $data['endtime'] < time()){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }

        //暂时写死！！！
        if($mk_id==4){

        }else{
            if($data['time_chose'] == 2 && $data['starttime'] > time()){
                $arr=['code'=>1,'msg'=>'活动尚未开始','data'=>''];
                return $arr;
            }
        }

        $set_data=[
            'member_id'=>$member,
            'coupon_id'=>$status['coupon_id'],
            'starttime'=>time(),
            'usestatus'=>0
        ];
        Db::startTrans();
        try {
            //对领取记录入库
            $da = db('coupondetails')->insert($set_data);
            //领取数量统计
            $shu = db('coupondetails')->where('coupon_id', $status['coupon_id'])->count();
            $gen = db('coupon')->where('coupon_id', $status['coupon_id'])->update(['coupon_alreadyreceived' => $shu]);


            //分享邀新
            if($mk_id==2 && isset($order_id) && isset($sharenum) && $sharenum>0){
                //将之前的被邀记录改写状态
                $where['member_id'] = $member;
                $where['order_id'] = $order_id;

                $count = Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>$order_id,'status'=>0])->count();
                $count = $count-$sharenum;

                if($count < 0){
                    $arr=['code'=>1,'msg'=>'领取失败','data'=>''];
                    return json_encode($arr);
                }

                //查出属于此红包的一个新会员id
                $new_member = Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>$order_id,'status'=>0])->limit($sharenum)->select();
                foreach ($new_member as $m_v) {
                    Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>$order_id,'new_member_id'=>$m_v['new_member_id']])->update(['status'=>1]);
                }

                if($count<$sharenum){
                    Db::name('share_register')->where($where)->update(['status'=>1, 'receivetime'=>time()]);
                }
            }elseif($mk_id==5){//常驻邀新
                $where['member_id'] = $member;
                $where['order_id'] = "longInvite";

                $count = Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>"longInvite",'status'=>0])->count();
                $count = $count-1;
                $temp_share = Db::name('share_register')->where($where)->find();

                //查出属于此红包的一个新会员id
                $new_member = Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>"longInvite",'status'=>0])->find();
                Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>"longInvite",'new_member_id'=>$new_member['new_member_id']])->update(['status'=>1]);

                if($count<$temp_share['sharenum']){
                    Db::name('share_register')->where($where)->update(['status'=>1, 'receivetime'=>time()]);
                }
            }else{
                Db::name('share_register')->where(['id' => $coupon_id])->update(['status' => 3, 'receivetime' => time()]);
            }

            Db::commit();
            $arr=['code'=>0,'msg'=>'领取成功','data'=>''];
            return json_encode($arr);
        }catch (Exception $exception){
            Db::rollback();

            $arr=['code'=>1,'msg'=>'领取失败','data'=>''];
            return json_encode($arr);
        }

    }


    //评论领优惠券
    public function sendCoupon(){
        $member = input('member_id');
        $goods_id=input('goods_id');
        $comment_id=input('comment_id');
        if(empty($member) || empty($goods_id) || empty($comment_id)){
            $arr=['code'=>1,'msg'=>'缺少参数member_id,comment_id或goods_id','data'=>''];
            return json_encode($arr);
        }
        $res=$this->couponMarket($member,3,'comment'.$comment_id,$goods_id);
        return json_encode($res);
    }

    //获取用户的优惠券列表
    public function listCoupon(){
        $member = input('member_id');

        $TokenModel  = new Token();
        $TokenModel->longInvite($member,5);//常驻邀新

        $data=Db::name('coupon_marketing')->where(['mk_id'=>2])->find();
        //先判断活动状态
        $status_prc=1;
        if($data['status'] == 2 ){
            $status_prc=2;
        }
        if($data['time_chose'] == 2 && $data['endtime'] < time()){
            $status_prc=2;
        }
        if($data['time_chose'] == 2 && $data['starttime'] > time()){
            $status_prc=2;
        }

        $data_com=Db::name('coupon_marketing')->where(['mk_id'=>3])->find();
        //先判断活动状态
        $status_comment=1;
        if($data_com['status'] == 2 ){
            $status_comment=2;
        }
        if($data_com['time_chose'] == 2 && $data_com['endtime'] < time()){
            $status_comment=2;
        }
        if($data_com['time_chose'] == 2 && $data_com['starttime'] > time()){
            $status_comment=2;
        }

        $data=Db::name('share_register')->where(['member_id'=>$member])->order('createtime desc')->select();
        $data_list=[];
        foreach ($data as $k){
            $cou_temp=Db::name('coupon')->where(['coupon_id'=>$k['coupon_id']])->find();
            if(empty($cou_temp)){
                continue;
            }

            if($k['type'] == 1){
                if($status_prc == 2 ){
                    $k['status'] = 4; //已过期
                }
                $k['title']='下单送好友红包';
                $k['num']=Db::name('share_register_log')->where(['member_id'=>$member,'order_id'=>$k['order_id'],'status'=>0])->count();
            }else if($k['type'] == 2){
                if($status_comment == 2 ){
                    $k['status'] = 4; //已过期
                }
                $k['title']='晒评论红包';
                $k['num']=1;
            }else if($k['type'] == 3){
                continue;
//                $k['title']='新年红包';
//                $where['member_id'] = $member;
//                $where['order_id'] = "新年活动";
//                $where['status'] = 0;
//                $k['num']=Db::name('share_register_log')->where($where)->count();
            }else if($k['type'] == 4){
                $k['title']='邀请好友得红包';

                $where['member_id'] = $member;
                $where['order_id'] = $k['order_id'];
                $where['status'] = 0;
                $k['num']=Db::name('share_register_log')->where($where)->count();
            }

            $k['type'] = $cou_temp['coupon_types']; //1折扣 2满减
            $k['price'] = $cou_temp['coupon_discount'];

            unset($k['createtime']);
            unset($k['receivetime']);
            if($k['status'] == 2){
                array_unshift($data_list,$k);
            }else{
                array_push($data_list,$k);
            }
        }
        if(empty($data_list)){
            $arr=['code'=>1,'msg'=>'还没有旅行红包可以领哦','data'=>''];
        }else{
            $arr=['code'=>0,'msg'=>'获取成功','data'=>$data_list];
        }
        return json_encode($arr);
    }



    //领取非拉红包
    public function flGetCoupon(){
        if($this->request->isPost()){
            $post=$this->request->post();
//            $coupon_id = $post['coupon_id'];
            $member_id = $post['member_id'];

            $coupon_id_arr = [20,21];//暂时写死

//            if (empty($coupon_id)) return json_encode(['code'=>101,'msg'=>'缺少红包id'],JSON_UNESCAPED_UNICODE);
            if (empty($member_id)) return json_encode(['code'=>101,'msg'=>'缺少用户名id'],JSON_UNESCAPED_UNICODE);

            $member = db("member")->where("member_id",$member_id)->find();
            if($member['member_pcode']!==1159){
                return json_encode(['code'=>101,'msg'=>'该红包仅限飞拉会员领取哦！'],JSON_UNESCAPED_UNICODE);
            }

            try{
                foreach ($coupon_id_arr as $coupon_id) {
                    //该红包是否足够本次派发 coupon_quantity
                    $coupon=Db::name('coupon')->where(['coupon_id'=>$coupon_id])->find();
                    $coupon_num=$coupon['coupon_quantity'] - $coupon['coupon_alreadyreceived'];
                    if($coupon_num < 1 && $coupon['num_types'] == 2 ){
                        return json_encode(['code'=>101,'msg'=>'领取失败，旅行红包数量不足'],JSON_UNESCAPED_UNICODE);
                    }

                    $receive_limit = $coupon['receive_limit'];
                    $receive_limit = $receive_limit>1 ? $receive_limit : 1;

                    $where_d['coupon_id'] = $coupon_id;
                    $where_d['member_id'] = $member_id;
                    $coupondetails = db("coupondetails")->where($where_d)->select();
                    $received_num = count($coupondetails);

                    if($received_num>=$receive_limit){
                        return json_encode(['code'=>101,'msg'=>'每位用户仅能领取'.$receive_limit.'张哦'],JSON_UNESCAPED_UNICODE);
                    }

                    //已领红包数
                    $count_del=$coupon['coupon_alreadyreceived'] + 1;
                    //领取红包详情
                    $coupondetailsData=[
                        'member_id'=>$member_id,
                        'coupon_id'=>$coupon_id,
                        'starttime'=>time(),
                        'usestatus'=>0
                    ];

                    Db::name('coupondetails')->insert($coupondetailsData);
                    Db::name('coupon')->where(['coupon_id'=>$coupon_id])->update(['coupon_alreadyreceived'=>$count_del]);
                }
                return json_encode(['code'=>0,'msg'=>'领取成功'],JSON_UNESCAPED_UNICODE);
            }catch ( \Exception $e){
                return json_encode(['code'=>101,'msg'=>'领取失败'],JSON_UNESCAPED_UNICODE);
            }

        }else{
            return json_encode(['code'=>101,'msg'=>'领取失败，缺少参数'],JSON_UNESCAPED_UNICODE);
        }
    }



    //领取暑期红包
    public function GetSummerCoupon(){
        if($this->request->isPost()){
            $post=$this->request->post();
            $member_id = $post['member_id'];

            $couponMarket = db("coupon_marketing")->where('mk_id',6)->find();
            $coupon_ids = $couponMarket['coupon_ids'];
            $coupon_id_arr = explode(",",$coupon_ids);

            if (empty($member_id)) return json_encode(['code'=>101,'msg'=>'缺少用户名id'],JSON_UNESCAPED_UNICODE);
            try{
                foreach ($coupon_id_arr as $coupon_id) {
                    //该红包是否足够本次派发 coupon_quantity
                    $coupon=Db::name('coupon')->where(['coupon_id'=>$coupon_id])->find();
                    $coupon_num=$coupon['coupon_quantity'] - $coupon['coupon_alreadyreceived'];
                    if($coupon_num < 1 && $coupon['num_types'] == 2 ){
                        return json_encode(['code'=>101,'msg'=>'领取失败，旅行红包数量不足'],JSON_UNESCAPED_UNICODE);
                    }

                    $receive_limit = $coupon['receive_limit'];
                    $receive_limit = $receive_limit>1 ? $receive_limit : 1;

                    $where_d['coupon_id'] = $coupon_id;
                    $where_d['member_id'] = $member_id;
                    $coupondetails = db("coupondetails")->where($where_d)->select();
                    $received_num = count($coupondetails);

                    if($received_num>=$receive_limit){
                        return json_encode(['code'=>101,'msg'=>'每位用户仅能领取'.$receive_limit.'张哦'],JSON_UNESCAPED_UNICODE);
                    }

                    //已领红包数
                    $count_del=$coupon['coupon_alreadyreceived'] + 1;
                    //领取红包详情
                    $coupondetailsData=[
                        'member_id'=>$member_id,
                        'coupon_id'=>$coupon_id,
                        'starttime'=>time(),
                        'usestatus'=>0
                    ];

                    Db::name('coupondetails')->insert($coupondetailsData);
                    Db::name('coupon')->where(['coupon_id'=>$coupon_id])->update(['coupon_alreadyreceived'=>$count_del]);
                }
                return json_encode(['code'=>0,'msg'=>'暑期红包已收到！'],JSON_UNESCAPED_UNICODE);
            }catch ( \Exception $e){
                return json_encode(['code'=>101,'msg'=>'领取失败'],JSON_UNESCAPED_UNICODE);
            }
        }else{
            return json_encode(['code'=>101,'msg'=>'领取失败，缺少参数'],JSON_UNESCAPED_UNICODE);
        }
    }


}